//app.js
const config = require('/evn/index.js')
const env = 'Dev'; //Dev Prod
App.config = config[env];

import {
  request,
  requestHasToken
} from '/service/network.js'
App({
  onLaunch: function () {
    wx.login({
      success: (res) => {
        if (res.code) {
          /* var d = this.globalData; //这里存储了appid、secret、token串  
          var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code';
          wx.request({
            url: l,
            data: {},
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
            // header: {}, // 设置请求的 header   
            success: function (res) {
              // var obj = {};
              // obj.openid = res.data.openid;
              // obj.expires_in = Date.now() + res.data.expires_in;
              // console.log(obj);
              // wx.setStorageSync('user', obj);//存储openid  
              wx.setStorageSync('openId', res.data.openid);//存储openid  
              request({
                url: 'api/nt/xcx/loginByOpenId',
                data: {
                  openId:res.data.openid,
                }
              }).then((loginres) => {
                const logindata = loginres.data
                if (logindata.code == 0) {
                  wx.setStorageSync('token', logindata.access_token)
                }
              })
            }
          }); */
          request({
            url: 'api/nt/xcx/loginByOpenId',
            data: {
              code:res.code,
              openId:''
            }
          }).then((loginres) => {
            const logindata = loginres.data
            console.log(logindata);
            if (logindata.code == 0) {
              wx.setStorageSync('openId', logindata.openId)
              wx.setStorageSync('userInfo', logindata.userinfo)
              wx.setStorageSync('token', logindata.access_token)
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },
  globalData: {
    appid: "wx64ca8f3f1bde995f",
    secret: "67b79b681bf76e0a1d73668e9dfa4181",
  }
})